package com.appbee.cm.client.regionunit.ui;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.appbee.cm.client.regionunit.presenter.RegionActivity;
import com.appbee.cm.client.regionunit.presenter.RegionActivity.ModulPresenter;
import com.appbee.user.client.AccessControl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.IsWidget;

public class RegionView extends Composite implements RegionActivity.View
{

	private RegionActivity presenter;

	private final FlowPanel panel = new FlowPanel();

	private final List<RegionActivity.ModulView> activitePanels = new ArrayList<RegionActivity.ModulView>();

	private final AccessControl accessControl;

	private final IRegionStyle regionStyle;

	@Inject
	public RegionView(AccessControl accessControl, IRegionStyle regionStyle)
	{
		this.accessControl = accessControl;
		this.regionStyle = regionStyle;
		initWidget(panel);

	}

	@Override
	public void setPresenter(RegionActivity presenter)
	{
		this.presenter = presenter;
	}

	@Override
	public List<RegionActivity.ModulView> createActivityPanels(int n)
	{
		while (activitePanels.size() < n)
		{
			RegionUnitView activityPanel = new RegionUnitView();
			activitePanels.add(activityPanel);
			panel.add(activityPanel);
		}
		while (activitePanels.size() > n)
		{
			RegionUnitView activityPanel = (RegionUnitView) activitePanels
					.remove(activitePanels.size() - 1);
			panel.remove(activityPanel);
		}

		return activitePanels;
	}

	private class RegionUnitView extends FlowPanel implements AcceptsOneWidget,
			RegionActivity.ModulView
	{
		private IsWidget currentWidget;
		private ModulPresenter modulPresenter;

		public RegionUnitView()
		{
			setStyleName(regionStyle.modulBlock());
			if (accessControl.isMemberOrAdmin())
			{
				FlowPanel buttonBar = new FlowPanel();
				buttonBar.setStyleName(regionStyle.buttonBar());
				add(buttonBar);
				Button addButton = new Button("Add");
				addButton.setStyleName(regionStyle.addButton());
				buttonBar.add(addButton);
				Button editButton = new Button("Edit");
				editButton.setStyleName(regionStyle.editButton());
				buttonBar.add(editButton);
				Button delButton = new Button("Delete");
				delButton.setStyleName(regionStyle.deleteButton());
				buttonBar.add(delButton);
				addButton.addClickHandler(new ClickHandler()
				{

					@Override
					public void onClick(ClickEvent event)
					{
						presenter.doAdd(event.getSource());
					}
				});
				editButton.addClickHandler(new ClickHandler()
				{

					@Override
					public void onClick(ClickEvent event)
					{
						modulPresenter.doEdit(event.getSource());
					}
				});
				delButton.addClickHandler(new ClickHandler()
				{

					@Override
					public void onClick(ClickEvent event)
					{
						modulPresenter.doDelete();
					}
				});
			}
		}

		@Override
		public void setWidget(IsWidget w)
		{
			if (currentWidget != null)
				remove(currentWidget);

			currentWidget = w;
			add(w);
		}

		@Override
		public void setPresenter(ModulPresenter presenter)
		{
			modulPresenter = presenter;

		}

		@Override
		public void setStyle(String style)
		{
			super.setStyleName(regionStyle.modulBlock() + " " + style);
		}

	}

}
